package com.mdxl.shiro.dao;

import com.mdxl.shiro.entity.SysPermission;
import com.mdxl.shiro.entity.SysRole;
import com.mdxl.shiro.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

/**
 * @Author:MuJiuTian
 * @Description:
 * @Date: Created in 下午4:33 2019/11/4
 */
@Mapper
public interface UserMapper {
    @Insert({
            "insert into sys_user(username,password,salt,createTime) values (#{username},#{password},#{salt},#{creat})"
    })
    int regist(Map<String,Object> map);

    @Select({
            "select id,username,password,salt from sys_user where username = #{username}"
    })
    @Results({
            @Result(column = "id",property = "id",jdbcType = JdbcType.INTEGER),
            @Result(column = "username",property = "username",jdbcType = JdbcType.VARCHAR),
            @Result(column = "password",property = "password",jdbcType = JdbcType.VARCHAR),
            @Result(column = "salt",property = "salt",jdbcType = JdbcType.VARCHAR)
    })
    User findByUserName(@Param("username") String username);

    /**
     * 查询一个用户多少个role
     */
    @Select({
           "select * from sys_role o left join sys_user_role r on o.id = r.role_id and r.user_id = #{id}"
    })
    @Results({
            @Result(column = "id",property = "id",jdbcType = JdbcType.INTEGER),
            @Result(column = "role_name",property = "roleName",jdbcType = JdbcType.VARCHAR),
            @Result(column = "available",property = "available",jdbcType = JdbcType.INTEGER),
    })
    List<SysRole> getRoleList(@Param("id") int id);

    /**
     * 查询一个role下有多少权限
     */
    @Select({
            "SELECT s.permission_name,s.permission FROM sys_permission s LEFT JOIN sys_role_permission p ON s.id = p.permission_id AND p.role_id = #{roleId}"
    })
    @Results({
            @Result(column = "permission_name",property = "permissionName",jdbcType = JdbcType.INTEGER),
            @Result(column = "permission",property = "permission",jdbcType = JdbcType.INTEGER)
    })
    List<SysPermission> getPermissionList(@Param("roleId") int roleId);
}
